﻿using System;
using System.Text;
using System.Web.Mvc;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;
using YouWei.Utility;

namespace YouWei.Op.Controllers
{
    /// <summary>
    /// 斑马车生活 短信着陆页
    /// </summary>
    public class ActivityMessageController : BaseController
    {
        CarlifeActivityService carService = new CarlifeActivityService();
        CarlifeActivity activityInfo = new CarlifeActivity()
        {
            CreateDate = DateTime.Now,
            UpdateDate = DateTime.Now,
            WelDetail = "随心所动，悦无止境，这里是“北京宝马MINI车友群”！同是MINI车主，同在北京，聚在一起聊车，聊美食，聊旅行，聊生活！聚集各行业精英，车友间整合资源，互助互利，一起享受更精彩的汽车生活！赶快扫码入群吧",
            HeadImageUrl = "/images/nodata.png",
            WxQRImageUrl = "/images/nodata.png",
            Image1Url = "/images/nodata.png",
            Image2Url = "/images/nodata.png",
            Image3Url = "/images/nodata.png",
            Image4Url = "/images/nodata.png"
        };

        // GET: ActivityMessage
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ContentResult List(CarlifeActivityModel model)
        {
            if (model.PageIndex < 1)
            {
                model.PageIndex = 1;
            }
            if (model.PageSize < 1)
            {
                model.PageSize = 20;
            }
            //返回
            ResultData<Page<CarlifeActivity>> rt = new ResultData<Page<CarlifeActivity>>();
            rt.data = carService.Pages(model);
            return ReturnResult(rt);
        }

        public ActionResult Detail()
        {
            string id = GetParam("id");
            if (!string.IsNullOrEmpty(id))
            {
                activityInfo = carService.SingleOrDefault(Convert.ToInt32(id));
                string savePath = ConfigHelper.GetAppSettingValue("ActivityUrlPath");
                if(!string.IsNullOrEmpty(activityInfo.HeadImageUrl))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.HeadImageUrl;
                }
                if (!string.IsNullOrEmpty(activityInfo.WxQRImageUrl))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.WxQRImageUrl;
                }
                if (!string.IsNullOrEmpty(activityInfo.Image1Url))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.Image1Url;
                }
                if (!string.IsNullOrEmpty(activityInfo.Image2Url))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.Image2Url;
                }
                if (!string.IsNullOrEmpty(activityInfo.Image3Url))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.Image3Url;
                }
                if (!string.IsNullOrEmpty(activityInfo.Image4Url))
                {
                    activityInfo.HeadImageUrl = savePath + activityInfo.Image4Url;
                }
            }
            return View(activityInfo);
        }

        public ActionResult Save()
        {
           
            activityInfo.CarName = GetParam("CarName");
            activityInfo.CarShortName = GetParam("CarShortName");
            activityInfo.WelContent = GetParam("WelContent");
            activityInfo.WelDetail = GetParam("WelDetail");
            activityInfo.WxCode = GetParam("WxCode");
            

            string id = GetParam("ID");
            activityInfo.ID = Convert.ToInt32(id);
            if (activityInfo.ID > 0)
            {
                carService.Update(activityInfo);
            }
            else
            {
                carService.Insert(activityInfo);
            }

            CreateHtml();
            //返回
            ResultData<string> rt = new ResultData<string>();
            return ReturnResult(rt);
        }

        private void CreateHtml()
        {
            string htmlPath = ConfigHelper.GetAppSettingValue("ActivityHtmlPath") + activityInfo.ID + ".html";
            string htmltpl = ConfigHelper.GetAppSettingValue("ActivityHtmlTpl");
            string tpl = System.IO.File.ReadAllText(htmltpl);
            tpl = tpl.Replace("{HeadImageUrl}", activityInfo.HeadImageUrl);
            tpl = tpl.Replace("{CarName}", activityInfo.CarName);
            tpl = tpl.Replace("{WxQRImageUrl}", activityInfo.WxQRImageUrl);
            tpl = tpl.Replace("{WxQRImageUrl}", activityInfo.HeadImageUrl);
            tpl = tpl.Replace("{Image1Url}", activityInfo.Image1Url);
            tpl = tpl.Replace("{Image2Url}", activityInfo.Image2Url);
            tpl = tpl.Replace("{Image3Url}", activityInfo.Image3Url);
            tpl = tpl.Replace("{Image4Url}", activityInfo.Image4Url);

            if (System.IO.File.Exists(htmlPath))
            {
                System.IO.File.Delete(htmlPath);
            }

            using (var fs = System.IO.File.Create(htmlPath))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes(tpl);
                fs.Write(info, 0, info.Length);
            }
        }

       public ActionResult UploadImage()
        {
            string savePath = ConfigHelper.GetAppSettingValue("ActivityPath");
            //返回
            ResultData<string> rt = new ResultData<string>();
          
            foreach (string name in Request.Files)
            {
                var f = Request.Files[name];
                if (f.ContentLength < 1)
                {
                    continue;
                }
                string fileName = Guid.NewGuid().ToString("N") + f.FileName.Substring(f.FileName.LastIndexOf("."), f.FileName.Length - f.FileName.LastIndexOf("."));
                var pathLast = savePath + fileName;
                f.SaveAs(pathLast);
                switch (name)
                {
                    case "HeadImageUrl":
                        activityInfo.HeadImageUrl = fileName;
                        break;
                    case "WxQRImageUrl":
                        activityInfo.WxQRImageUrl = fileName;
                        break;
                    case "Image1Url":
                        activityInfo.Image1Url = fileName;
                        break;
                    case "Image2Url":
                        activityInfo.Image2Url = fileName;
                        break;
                    case "Image3Url":
                        activityInfo.Image3Url = fileName;
                        break;
                    case "Image4Url":
                        activityInfo.Image4Url = fileName;
                        break;
                }
                rt.data = fileName;
              
            }

            return ReturnResult(rt);
        }
    }
}